package servlet;

import domain.Car;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.WebContext;
import util.TemplateEngineUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

//这是一个注解：定义了LoginServlet的访问形式是login
@WebServlet("/login")
public class loginservlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doPost(req, resp);
        }

        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            //获取模板引擎
            TemplateEngine engine= TemplateEngineUtil.getTemplateEngine(req.getServletContext());
            //构造传出去的web上下文环境
            WebContext context=new WebContext(req,resp,req.getServletContext());
            resp.setCharacterEncoding("utf-8");

            List cars=new ArrayList();//创建一个列表数组

            Car car1=new Car();
            car1.setBrand("宝马");
            car1.setSpeed(100);
            car1.setOil(10);
            cars.add(car1);

            Car car2=new Car();
            car2.setBrand("奔驰");
            car2.setSpeed(200);
            car2.setOil(10);
            cars.add(car2);

            Car car3=new Car();
            car3.setBrand("本田");
            car3.setSpeed(100);
            car3.setOil(10);
            cars.add(car3);

            Car car4=new Car();
            car4.setBrand("兰博基尼");
            car4.setSpeed(220);
            car4.setOil(10);
            cars.add(car4);

            req.setAttribute("cars",cars);
            engine.process("showCars.html",context,resp.getWriter());
        }

}
